//#define SampleLOD
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    [CustomEditor(typeof(MeshFilter))]
    public class MeshRendererEditor : Editor
    {
        private bool isBlockCamera = true;

        private float[] GetDftLodScreenSizes(int aNrOflevels)
        {
            switch (aNrOflevels)
            {
                case 1:
                    return new float[1] { 0.5f };
                case 2:
                    return new float[2] { 0.6f, 0.3f };
                case 3:
                    return new float[3] { 0.6f, 0.3f, 0.15f };
                case 4:
                    return new float[4] { 0.75f, 0.5f, 0.25f, 0.13f };
                default:
                    return new float[5] { 0.8f, 0.6f, 0.4f, 0.2f, 0.1f };
            }
        }

        //        [DllImport("User32")]
        //        private static extern bool SetCursorPos(int x, int y);

        private void OnSceneGUI()
        {
#if UNITY_EDITOR

            if (Application.isPlaying)
            {
                return;
            }

            MeshFilter filter = (MeshFilter)target;

            if (filter == null || filter.sharedMesh == null)
            {
                return;
            }

            Handles.BeginGUI();
            // var sceneSize = SceneView.currentDrawingSceneView;
            float width = 240f;
            float height = 85f;

            Rect rect = new Rect(3, 3, width, height);
            GUI.Box(rect, "");
            GUILayout.BeginArea(rect);
            string textVerts = "";
            string textTris = "";
            MonoBehaviour lodSwitcher = null;

#if SampleLOD
            lodSwitcher = filter.GetComponent<LODSwitcher>();
#endif

            if (lodSwitcher == null)
            {
                textVerts = $"Verts Count = {filter.sharedMesh.vertices.Length}";
                textTris = $"Tris Count = {filter.sharedMesh.triangles.Length / 3}";
            }
#if SampleLOD
            else
            {                                
                textVerts = $"Verts Count = {lodSwitcher.lodMeshes[0]?.vertices.Length}/{lodSwitcher.lodMeshes[1]?.vertices.Length}/{lodSwitcher.lodMeshes[2]?.vertices.Length}";
                textTris = $"Tris Count = {lodSwitcher.lodMeshes[0]?.triangles.Length / 3}/{lodSwitcher.lodMeshes[1]?.triangles.Length / 3}/{lodSwitcher.lodMeshes[2]?.triangles.Length / 3}";
            }
#endif

            GUILayout.Label(textVerts);
            GUILayout.Label(textTris);

#if SampleLOD
            GUILayout.BeginHorizontal();
            string textTmp = (lodSwitcher == null ?"生成LodMesh" : "重新生成LodMesh");
            if (GUILayout.Button(textTmp))
            {
                if (filter != null)
                {
                    SimpleLOD_EditorPopup.CreateLod(filter.gameObject, 2, new float[] {0.8f, 2f}, 1.5f);
                }
            }
            if (lodSwitcher != null)
            {
                if (GUILayout.Button("清除"))
                {
                    DestroyImmediate(lodSwitcher);
                }
            }         
            GUILayout.EndHorizontal();            
#endif

            GUILayout.BeginHorizontal();
            if (filter.GetComponent<Collider>() == null)
            {
                if (GUILayout.Button("添加碰撞"))
                {
                    filter.gameObject.AddComponent<MeshCollider>();
                }
            }
            GUILayout.EndHorizontal();


            GUILayout.EndArea();
            Handles.EndGUI();
#endif

        }

    }
}